package org.example.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.example.annotation.Log;
import org.example.domain.po.ErpOrganization;
import org.example.domain.po.ErpUser;
import org.example.domain.vo.ErpOrganizationVO;
import org.example.enums.BusinessType;
import org.example.service.ErpOrganizationService;
import org.example.utils.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RequestMapping("/organ")
@RestController
@Api("机构管理")
public class ErpOrganizationController {
    @Resource
    private ErpOrganizationService organService;
    
    @GetMapping("/findOrganTree")
    @ApiOperation("查询树形机构")
    public Result<List<ErpOrganizationVO>> findAll() {
        return Result.success(organService.selectOrganVOTree());
    }
    
    @GetMapping("/queryParent")
    @ApiOperation("查询机构的上级机构")
    public Result<List<ErpOrganizationVO>> queryParent(Long id, @RequestAttribute ErpUser user) {
        return Result.success(organService.queryParent(id, 0L, user.getTenantId()));
    }
    
    @Log(operation = "机构", businessType = BusinessType.UPDATE, isSaveRequestData = true, paramName = "organClick", jsonPath = "$.id")
    @PutMapping("/upOrgan")
    @ApiOperation("修改机构信息")
    public Result<String> update(@RequestBody ErpOrganization organClick) {
        boolean b = organService.updateOrgan(organClick);
        return b ? Result.success("操作成功") : Result.error("更新失败") ;
    }
    
    @Log(operation = "机构", businessType = BusinessType.DELETE, isSaveRequestData = true, paramName = "organAbs")
    @DeleteMapping("/batchRemoveOrgan/{organAbs}")
    @ApiOperation("批量删除机构")
    public Result<String> remove(@PathVariable List<String> organAbs, @RequestAttribute ErpUser user){
        boolean b = organService.removeBatchByOrgAbs(organAbs, user.getTenantId());
        return b ? Result.success("操作成功！") : Result.error("失败");
    }
    
    @Log(operation = "机构", businessType = BusinessType.INSERT, isSaveRequestData = true, paramName = "organClick", jsonPath = "$.orgAbr")
    @PostMapping("/organInsert")
    @ApiOperation("新增机构")
    public Result<String> save(@RequestBody ErpOrganization organClick, @RequestAttribute("user") ErpUser user) {
        organClick.setTenantId(user.getTenantId());
        boolean save = organService.organInsert(organClick);
        return save ? Result.success("操作成功") : Result.error("失败");
    }
    
    @GetMapping("/findOrgOne")
    @ApiOperation("查询机构名称")
    public Result<Boolean> findOrgOne(String orgAbr, @RequestAttribute ErpUser user){
        LambdaQueryWrapper<ErpOrganization> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ErpOrganization::getOrgAbr, orgAbr)
                .eq(ErpOrganization::getTenantId, user.getTenantId());
        long count = organService.count(wrapper);
        boolean h = count == 0;
        return Result.success(h);
    }
}
